package com.mofum.scope.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mofum.scope.annotation.service.ColumnScope;
import com.mofum.scope.annotation.service.TableScope;
import com.mofum.scope.dto.UserDto;
import com.mofum.scope.entity.Page;
import com.mofum.scope.entity.User;
import com.mofum.scope.entity.input.DpRoleMenuPageInput;
import com.mofum.scope.entity.output.DpRoleMenuOutput;
import com.mofum.scope.mapper.UserMapper;
import com.mofum.scope.service.IUserService;
import com.mofum.scope.util.BeanConver;

/**
 * 用户业务
 *
 * @author developer@omuao.com
 * @since 2019-03-28
 **/
@Service
@TableScope(columns = {
/**
 * @see com.mofum.scope.controller.ScopeController 中的extractorScopes()方法
 */
@ColumnScope(type = "ScopeOne", value = "scope_one") //配置列type 是类型，scope_one是表中的列
})
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public void initTable() {
        userMapper.createTable();
    }

    @Override
    public void addUser(User user) {
        userMapper.insertUser(user);
    }

    @Override
    public List<User> queryUser(UserDto user, Page page) {
        return userMapper.queryUser(user, page);
    }

    @Override
    public int delete(UserDto userDto) {
        return 0;
    }

    @Override
    public IPage<DpRoleMenuOutput> dpUserPage(com.baomidou.mybatisplus.extension.plugins.pagination.Page page,
                                              DpRoleMenuPageInput input) {
        IPage<DpRoleMenuOutput> outputIPage = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(
            page.getCurrent(), page.getSize());
        //构建查询条件
        User cond = User.builder().scopeCollections(input.getScopeCollections())
            .serviceIds(input.getServiceIds()).build();
        //        DpRoleMenu cond = DpRoleMenu.builder().build();
        QueryWrapper<User> query = Wrappers.query(cond);

        //更新时间倒叙排列
        query.lambda().orderByAsc(User::getId);

        //分页查询
        IPage<User> result = this.page(page, query);
        if (CollectionUtils.isEmpty(result.getRecords())) {
            return outputIPage;
        }
        //封装出参
        List<DpRoleMenuOutput> outputList = new ArrayList<>(result.getRecords().size());
        for (User user : result.getRecords()) {
            DpRoleMenuOutput output = BeanConver.copeBean(user, DpRoleMenuOutput.class);
            outputList.add(output);
        }
        //封装分页参数信息
        outputIPage.setRecords(outputList);
        outputIPage.setTotal(result.getTotal());
        return outputIPage;
    }
}
